<?php
// mysql 组件

use  Workerman\Worker;

require_once __DIR__ . '/../../vendor/autoload.php';

$worker = new Worker('JsonMySQL://0.0.0.0:8484');
$worker->onWorkerStart = function () {
    global $db;
    $db = new \Workerman\MySQL\Connection('localhost', '3306', 'root', 'root', 'bilibili');
};

$worker->onMessage = function ($connection, $data) {
    global $db;
    $sql = $data['sql'];
    $time = $data['time'];
    $result = $db->query($sql);
    $data = array(
        'result' => $result,
        'time' => $time
    );
    $connection->send(json_encode($data));
};

Worker::runAll();